package com.example.frn.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.frn.dao.LikeMapper;
import com.example.frn.dto.*;
import com.example.frn.entity.Review;
import com.example.frn.service.ReviewService;
import com.example.frn.util.JwtUtil;
import com.example.frn.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/v1/review")
public class ReviewController {
    @Autowired
    private ReviewService reviewService;
    @PostMapping("save")
    public R save(@RequestBody ReviewInsertDto dto){
       Review review = BeanUtil.copyProperties(dto,Review.class);
        if(reviewService.save(review)){
            return R.ok(true);
        }
        else return R.error(false);
    }
    @PostMapping("updateStatus/{rid}/{status}")
    public R updateStatus(@PathVariable Integer rid,@PathVariable Integer status){
        if(reviewService.updateStatus(rid,status)){
            return R.ok(true);
        }
        else return R.error(false);
    }
    @PostMapping("remove/{cid}")
    public R remove(@PathVariable("cid") Integer cid) {
        if (reviewService.removeById(cid))
            return R.ok(true);
        else
            return R.error(false);
    }
    @PostMapping("like/{uid}/{rid}")
    public R like(@PathVariable Integer uid,@PathVariable Integer rid){

        if(reviewService.updateLikeNum(uid, rid)){
            return R.ok(true);
        }
        else return R.error(false);
    }
    @GetMapping("detail/{cid}")
    public R detail(@PathVariable("cid") Integer cid){
//        if(token != null){
//           // Map<String,String> claims = JwtUtil.verify();
//            Review review = reviewService.getById(cid);
//            return R.ok(review);
//        }
        Review review = reviewService.getById(cid);
        return R.ok(review);
    }

    @PostMapping("page")
    public R page(@RequestBody ReviewPageDto dto){
        return R.ok(reviewService.page(dto));
    }

}

